#include <QtGui/QMouseEvent>
#include "GLWidget.h"

GLWidget::GLWidget(sVector *vGyro, sVector *vAcc, sVector *vMag, QWidget *parent) : QGLWidget(parent) {
  setMouseTracking(true);
  fAngleX = -20.;
  fAngleY = -10.;
  iTranslateZ = 400;
  bDrag = false;
  bRotate = false;

  this->vGyro= vGyro;
  this->vAcc = vAcc;
  this->vMag = vMag;
}

void GLWidget::initializeGL() {

  glEnable(GL_BLEND);
  glEnable(GL_POLYGON_SMOOTH);
  glEnable(GL_DEPTH_TEST);   // Enables Depth Testing
  glDepthFunc(GL_LEQUAL);      // The Type Of Depth Testing To Do
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glClearColor(0.7, 0.7, 0.7, 0);
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void GLWidget::resizeGL(int w, int h) {

  iWindowHigh = h;
  iWindowWidth = w;
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);
  glOrtho(0, w, 0, h,-1000.,1000.); // set origin to bottom left corner
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void GLWidget::paintGL() {
  glLoadIdentity();
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);


  glTranslatef(300.0f,250.,iTranslateZ);

  glRotatef(fAngleX,1.,0.,0.);
  glRotatef(fAngleY,0.,1.,0.);
  //glRotatef(fAngleZ,0.,0.,1.);

  for (GLfloat i = -100; i <= 100; i += 10){
    glBegin(GL_LINES);
    glColor4f(0.,0.,1.,5.);    // Y-Grid
    glVertex3f(i , 0. , -100.);
    glVertex3f(i , 0. , 100.);
    glColor4f(1.,0.,0.,5.);    // X-Grid
    glVertex3f(-100. , 0 , i);
    glVertex3f( 100. , 0 , i);
    glEnd();
  }

  double rotatex = ((double) vGyro->x / (32767.)) * 500.;
  glRotatef(((double) vGyro->x / (32767.)) * 500.,0.,0.,1.);
  glRotatef(((double) vGyro->y / (32767.)) * 500.,1.,0.,0.);
  glRotatef(((double) vGyro->z / (32767.)) * 500. ,0.,1.,0.);
  //glRotatef(30, 1.,0.,0.);


  glBegin(GL_QUADS);									// Draw A Quad
  glColor4f(0.0f,1.0f,0.0f,1.);						// Set The Color To Green
  glVertex3f( 20.0f, 3.0f,10.0f);					// Top Right Of The Quad (Top)
  glVertex3f(-20.0f, 3.0f,10.0f);					// Top Left Of The Quad (Top)
  glVertex3f(-20.0f, 3.0f, -10.0f);					// Bottom Left Of The Quad (Top)
  glVertex3f( 20.0f, 3.0f, -10.0f);					// Bottom Right Of The Quad (Top)

  glColor3f(1.0f,0.5f,0.0f);						// Set The Color To Orange
  glVertex3f( 20.0f,-3.0f,-10.0f);					// Bottom Right Of The Quad (Bottom)
  glVertex3f(-20.0f,-3.0f,-10.0f);					// Bottom Left Of The Quad (Bottom)
  glVertex3f(-20.0f,-3.0f, 10.0f);					// Top Left Of The Quad (Bottom)
  glVertex3f( 20.0f,-3.0f, 10.0f);					// Top Right Of The Quad (Bottom)

  glColor3f(1.0f,0.0f,0.0f);						// Set The Color To Red
  glVertex3f( 20.0f, -3.0f, 10.0f);					// Top Right Of The Quad (Front)
  glVertex3f(-20.0f, -3.0f, 10.0f);					// Top Left Of The Quad (Front)
  glVertex3f(-20.0f,3.0f, 10.0f);					// Bottom Left Of The Quad (Front)
  glVertex3f( 20.0f,3.0f, 10.0f);					// Bottom Right Of The Quad (Front)
  glColor3f(1.0f,1.0f,0.0f);						// Set The Color To Yellow
  glVertex3f( 20.0f, 3.0f,-10.0f);					// Top Right Of The Quad (Back)
  glVertex3f(-20.0f, 3.0f,-10.0f);					// Top Left Of The Quad (Back)
  glVertex3f(-20.0f, -3.0f,-10.0f);					// Bottom Left Of The Quad (Back)
  glVertex3f( 20.0f, -3.0f,-10.0f);					// Bottom Right Of The Quad (Back)
  glColor3f(0.0f,0.0f,1.0f);						// Set The Color To Blue
  glVertex3f(-20.0f, 3.0f,-10.0f);					// Top Right Of The Quad (Left)
  glVertex3f(-20.0f, 3.0f, 10.0f);					// Top Left Of The Quad (Left)
  glVertex3f(-20.0f,-3.0f, 10.0f);					// Bottom Left Of The Quad (Left)
  glVertex3f(-20.0f,-3.0f,-10.0f);					// Bottom Right Of The Quad (Left)
  glColor3f(1.0f,0.0f,1.0f);						// Set The Color To Violet
  glVertex3f( 20.0f, 3.0f, 10.0f);					// Top Right Of The Quad (Right)
  glVertex3f( 20.0f, 3.0f,-10.0f);					// Top Left Of The Quad (Right)
  glVertex3f( 20.0f,-3.0f,-10.0f);					// Bottom Left Of The Quad (Right)
  glVertex3f( 20.0f,-3.0f, 10.0f);					// Bottom Right Of The Quad (Right)
  glEnd();											// Done Drawing The Quad

}


void GLWidget::mousePressEvent(QMouseEvent *event) {

  if(event->button() == Qt::LeftButton)
    bRotate = true;
}

void GLWidget::mouseReleaseEvent(QMouseEvent *event) {

  if(event->button() == Qt::LeftButton)
    bRotate = false;
}

void GLWidget::mouseMoveEvent(QMouseEvent *event) {

  if( bRotate){
    fAngleY -= event->x() - MouseLastPosition.iX;
    fAngleX -= event->y() - MouseLastPosition.iY;
  }
  MouseLastPosition.iX = event->x();
  MouseLastPosition.iY = event->y();
}

void GLWidget::keyPressEvent(QKeyEvent* event) {
  switch(event->key()) {
  case Qt::Key_Escape:
    close();
    break;
  default:
    event->ignore();
    break;
  }
}

void GLWidget::wheelEvent(QWheelEvent *event){
  iTranslateZ -= event->delta() / 10;
}